<?php

require_once APP_PATH . '/app/common/hisql.php';
require_once APP_PATH . '/app/common/hilog.php';

class UserModel
{

    protected $m_sql = '';
    protected $m_sqli = '';

    function __construct()
    {
        global $config;
        $dbconfig = &$config['database'][0];
        hilog::info("db: " . json_encode($dbconfig));

        /* $this->m_sql = mysql_connect($dbconfig['host'] . ':' . $dbconfig['port'], $dbconfig['user'], $dbconfig['password']);
          if (!$this->m_sql) {
          set_last_error(-1, 'Could not connect: ' . mysql_error());
          return false;
          }
          mysql_select_db($dbconfig['database'], $this->m_sql);
          mysql_set_charset($dbconfig['charset'], $this->m_sql); */

        $this->m_sqli = new mysqli($dbconfig['host'], $dbconfig['user'], $dbconfig['password'], $dbconfig['database'], $dbconfig['port']);
        if (mysqli_connect_error()) {
            set_last_error(-1, 'Connect Error (' . mysqli_connect_errno() . ') '
                . mysqli_connect_error());
            return false;
        }
        $this->m_sqli->select_db($dbconfig['database']);
        $this->m_sqli->set_charset($dbconfig['charset']);
    }

//登陆检查
    function check($param)
    { //test
        $selectlist = array(
            'userpwd' => 'string'
        );
        global $config;
        $dbconfig = $config['database'][0];
        $sql = hisql::db($dbconfig['host'], $dbconfig['user'], $dbconfig['password'], $dbconfig['port'], $dbconfig['database'], $dbconfig['charset']);
        $q = "select userpwd from coupon_user where phone = '" . $param['phone'] . "'";

        $result = hisql::query($sql, $q, $selectlist);
        if (!$result) {
            set_last_error(-1, '用户不存在！');
            return false;
        }
        foreach ($result as $row) {
            if ($row['userpwd'] != $param['password']) {
                set_last_error(-2, '密码错误，请重试!');
                return false;
            }
        }
        return true;
    }

//获取用户信息
    function getInfo($param)
    {
        $selectlist = array(
            'uid' => 'string',
            'username' => 'string',
            'role' => 'string',
            'role_name' => 'string',
            'app' => 'string'
        );
        global $config;
        $dbconfig = $config['database'][0];
        $sql = hisql::db($dbconfig['host'], $dbconfig['user'], $dbconfig['password'], $dbconfig['port'], $dbconfig['database'], $dbconfig['charset']);
        $q = "select uid, username, role, role_name, app from coupon_user where phone = '" . $param['phone'] . "'";
        $result = hisql::query($sql, $q, $selectlist);
        if (!$result) {
            set_last_error(-1, '用户不存在！');
            return false;
        }
        $info = [];
        foreach ($result as $row) {
            $info = array(
                'uid' => $row['uid'],
                'username' => $row['username'],
                'role' => $row['role'],
                'role_name' => $row['role_name'],
                'app' => empty($row['app']) ? '1000' : $row['app']
            );
        }

        //获取角色权限
        $q = "select authority from coupon_role where role = '" . $info['role'] . "'";
        $selectlist = array(
            'authority' => 'string'
        );
        $result = hisql::query($sql, $q, $selectlist);
        if (!$result) {
            set_last_error(-1, '角色不存在！');
            return false;
        }
        foreach ($result as $row) {
            $info['authority'] = $row['authority'];
        }
        return $info;
    }
}